<template>
    <el-dialog
        title="提示"
        :visible="isOpen"
        width="50%"
        @close="$emit('closeDialog')"
        >
        <el-form :model="bannerData" :rules="rules" ref="bannerForm"> 
            <el-form-item label="活动名称"  prop="active_name">
                <el-input v-model="bannerData.active_name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="活动地址"  prop="url">
                <el-input v-model="bannerData.url" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="上传图片" prop="image">
                <el-upload
                    class="avatar-uploader"
                    action="#"
                    :show-file-list="false"
                    :http-request="uploadImage"
                    >
                    <img v-if="bannerData.image" :src="bannerData.image" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="开始时间" prop="start_time">
                <el-date-picker
                    v-model="bannerData.start_time"
                    type="date"
                    placeholder="选择日期"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                >
                </el-date-picker>
            </el-form-item>

            <el-form-item label="结束时间"  prop="end_time">
                <el-date-picker
                    v-model="bannerData.end_time"
                    type="date"
                    placeholder="选择日期"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                >
                </el-date-picker>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="$emit('closeDialog')">取 消</el-button>
            <el-button type="primary" @click="addBanner">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import api from '@/api';
import {bannerRules} from '@/config/rules'
export default {
    props:{
        isOpen:{
            type:Boolean,
            default:false
        },
        initbannerData:{
            type:Object,
            default:()=>({})
        }
    },
    data(){
        return {
            bannerData:{
                active_name:'',
                url:'',
                image:'',
                start_time:'',
                end_time:'',
                user_id:''
            },
            rules:bannerRules
        }
    },
    watch:{
        isOpen(val){
            if(val){
                if(this.initbannerData){
                    this.bannerData = {...this.initbannerData};
                }else{
                    this.bannerData = {
                        active_name:'',
                        url:'',
                        image:'',
                        start_time:'',
                        end_time:'',
                        user_id:''
                    }
                }
            }
        }
    },
    methods:{
        uploadImage(e){
            //图片对象
            let file = e.file;
            //window 属性FormData 携带传递的图片或文件信息
            let formData = new FormData();
            formData.append('file',file);
            api.uploadImage(formData).then(res => {
                this.bannerData.image = process.env.VUE_APP_IMAGEURL + res.data.filename
            })
        },
        //添加banner
        addBanner(){
            this.$refs['bannerForm'].validate(() => {
                this.$emit('submitBanner',this.bannerData)
            });
        }
    }
}
</script>

<style lang="scss">
    .avatar-uploader .el-upload{
        width:180px;
        height: 180px;
        border:1px dashed gray;
        border-radius: 10px;
        text-align: center;
        line-height: 180px;
        overflow: hidden;
    }

    .el-upload img{
        max-width:100%;
        max-height: 100%;
    }
</style>
